#include "pimage.h"

//***************************************************************************** Constructor

PImage::PImage(QWidget *parent): QMainWindow(parent), ui(new Ui::PImageClass){
    ui->setupUi(this);
    // Creation TabWidget
    tabDrawingArea = new QTabWidget(ui->centralWidget);
    //Add the TabWidget in the MainWindows
    this->setCentralWidget(tabDrawingArea);
    tabDrawingArea->setTabsClosable(true);
    tabDrawingArea->setMovable(true);
    layerCopy = NULL;
    redimension = NULL;
    newProjectCount = 1;
    //******************************************************************  Connection Action Menu with the Slot
    // Menu Fichier
    QObject::connect(ui->actionNewProject, SIGNAL(triggered()), this, SLOT(newProject()));
    QObject::connect(ui->actionOpen, SIGNAL(triggered()), this, SLOT(open()));
    QObject::connect(ui->actionSaveAs, SIGNAL(triggered()), this, SLOT(saveAs()));
    QObject::connect(ui->actionSave, SIGNAL(triggered()), this, SLOT(save()));
    QObject::connect(ui->actionFermer, SIGNAL(triggered()), this, SLOT(closeDrawing()));
    QObject::connect(ui->actionQuit, SIGNAL(triggered()), this, SLOT(quit()));
    // Menu Edition
    QObject::connect(ui->actionCopier, SIGNAL(triggered()), this, SLOT(copy()));
    QObject::connect(ui->actionColler, SIGNAL(triggered()), this, SLOT(paste()));
    QObject::connect(ui->actionSelectAll, SIGNAL(triggered()), this, SLOT(selectAll()));
    // Menu Image
    QObject::connect(ui->actionRedimensionner, SIGNAL(triggered()), this, SLOT(redimensionner()));
    QObject::connect(ui->actionMiroirHoriz, SIGNAL(triggered()),this,SLOT(MiroirHorizontal()));
    QObject::connect(ui->actionMiroirVertical, SIGNAL(triggered()),this,SLOT(MiroirVertical()));
    QObject::connect(ui->actionRotation90sensHoraire, SIGNAL(triggered()),this,SLOT(rotation90Horaire()));
    QObject::connect(ui->actionRotation90sensAntiHoraire, SIGNAL(triggered()),this,SLOT(rotation90AntiHoraire()));
    // Menu Histogramme
    QObject::connect(ui->actionAfficherHistogramme, SIGNAL(triggered()), this, SLOT(showHistogram()));
    QObject::connect(ui->actionEgaliserHistogramme, SIGNAL(triggered()), this, SLOT(showHistEgalize()));
    QObject::connect(ui->actionEtalerHistogramme, SIGNAL(triggered()), this, SLOT(showHistSpread()));
    QObject::connect(ui->actionRecadrerHistogramme, SIGNAL(triggered()), this, SLOT(showHistShift()));
    QObject::connect(ui->actionBinariserHistogramme, SIGNAL(triggered()), this, SLOT(showHistBin()));
    // Menu Filtre
    QObject::connect(ui->actionMedian, SIGNAL(triggered()), this, SLOT(median()));
    QObject::connect(ui->actionAttenuate, SIGNAL(triggered()), this, SLOT(attenuate()));
    QObject::connect(ui->actionDesaturate, SIGNAL(triggered()), this, SLOT(desaturate()));
    QObject::connect(ui->actionConvolution, SIGNAL(triggered()), this, SLOT(convolution()));
    QObject::connect(ui->actionNegative, SIGNAL(triggered()), this, SLOT(negative()));
    QObject::connect(ui->actionSobel, SIGNAL(triggered()), this, SLOT(sobel()));
    QObject::connect(ui->actionUpOutline, SIGNAL(triggered()), this, SLOT(upOutline()));
    QObject::connect(ui->actionNoise, SIGNAL(triggered()), this, SLOT(noise()));
    // Menu Affichage
    QObject::connect(ui->actionShowOutils, SIGNAL(triggered()), this, SLOT(toolsWidgetShow()));
    QObject::connect(ui->dockTools, SIGNAL(visibilityChanged(bool)), this, SLOT(MenuAffichageUpdate()));
    QObject::connect(ui->actionShowCalques, SIGNAL(triggered()), this, SLOT(CalquesShow()));
    QObject::connect(ui->dockCalques, SIGNAL(visibilityChanged(bool)), this, SLOT(MenuAffichageUpdate()));
    QObject::connect(ui->actionShowColors, SIGNAL(triggered()), this, SLOT(ColorShow()));
    QObject::connect(ui->dockColors, SIGNAL(visibilityChanged(bool)), this, SLOT(MenuAffichageUpdate()));
    // Menu Layers
    QObject::connect(ui->actionNewLayer, SIGNAL(triggered()), this, SLOT(addNewLayer()));
    QObject::connect(ui->actionRemoveLayer, SIGNAL(triggered()), this, SLOT(removeLayer()));
    QObject::connect(ui->actionSelectTheLowerLayer, SIGNAL(triggered()), this, SLOT(selectTheLowerLayer()));
    QObject::connect(ui->actionSelectTheUpperLayer, SIGNAL(triggered()), this, SLOT(selectTheUpperlayer()));
    QObject::connect(ui->actionSelectTheLowestLayer, SIGNAL(triggered()), this, SLOT(selectTheLowestLayer()));
    QObject::connect(ui->actionSelectTheUppestLayer, SIGNAL(triggered()), this, SLOT(selectTheUppestLayer()));
    QObject::connect(ui->actionTakeDownTheLayer, SIGNAL(triggered()), this, SLOT(takeDownTheLayer()));
    QObject::connect(ui->actionTakeUpTheLayer, SIGNAL(triggered()), this, SLOT(takeUpTheLayer()));
    // Menu Fusion
    QObject::connect(ui->actionLayerFusionVisibleSizeUnion, SIGNAL(triggered()), this, SLOT(fusionVisibleLayersUnion()));
    QObject::connect(ui->actionLayerFusionVisibleSizeImage, SIGNAL(triggered()), this, SLOT(fusionVisibleLayersFitImage()));
    QObject::connect(ui->actionLayerFusionVisibleSizeIntersect, SIGNAL(triggered()), this, SLOT(fusionVisibleLayersIntersection()));
    // Gestion a tab
    QObject::connect(tabDrawingArea, SIGNAL(tabCloseRequested(int)), this, SLOT(closeDrawing(int)));
    QObject::connect(tabDrawingArea, SIGNAL(currentChanged(int)), this, SLOT(changementTab()));
    // Gestion Zoom
    QObject::connect(ui->actionZoomPlus, SIGNAL(triggered()), this, SLOT(zoomPlus()));
    QObject::connect(ui->actionZoomMoin, SIGNAL(triggered()), this, SLOT(zoomMoin()));
    // Menu Aide
    QObject::connect(ui->actionAProposDe, SIGNAL(triggered()), this, SLOT(aPropos()));

    //****************************************************************** Creation Of Docks
    createTools();
    createCalque();
    createDockColors();

    //****************************************************************** EnableButton
    enableButtonMenu();
}

//***************************************************************************** Enable the button

void PImage::enableButtonMenu(){
    bool existDrawing;
    if(tabDrawingArea->count() == 0){
        existDrawing = false;
    }else{
        existDrawing = true;
    }

    // Enable button tools
    uiTools->actionCleverSelected->setEnabled(existDrawing);
    uiTools->actionCrop->setEnabled(existDrawing);
    uiTools->actionPipette->setEnabled(existDrawing);
    uiTools->actionMove->setEnabled(existDrawing);
    uiTools->actionSelectedRect->setEnabled(existDrawing);
    uiTools->actionMoveSelection->setEnabled(existDrawing);
    uiTools->actionPencil->setEnabled(existDrawing);

    // Menu Fichier
    ui->actionSave->setEnabled(existDrawing);
    ui->actionSaveAs->setEnabled(existDrawing);
    ui->actionFermer->setEnabled(existDrawing);

    // Zoom
    ui->actionZoomMoin->setEnabled(existDrawing);
    ui->actionZoomPlus->setEnabled(existDrawing);

    //Menu Edition
    ui->actionUndo->setEnabled(existDrawing);
    ui->actionRedo->setEnabled(existDrawing);
    ui->actionCut->setEnabled(existDrawing);
    ui->actionColler->setEnabled(existDrawing);
    ui->actionCopier->setEnabled(existDrawing);
    ui->actionSelectAll->setEnabled(existDrawing);

    // Menu Image
    ui->actionRedimensionner->setEnabled(existDrawing);
    ui->actionMiroirHoriz->setEnabled(existDrawing);
    ui->actionMiroirVertical->setEnabled(existDrawing);
    ui->actionRotation90sensHoraire->setEnabled(existDrawing);
    ui->actionRotation90sensAntiHoraire->setEnabled(existDrawing);

    // Menu Histogramme
    ui->actionAfficherHistogramme->setEnabled(existDrawing);
    ui->actionEgaliserHistogramme->setEnabled(existDrawing);
    ui->actionEtalerHistogramme->setEnabled(existDrawing);
    ui->actionRecadrerHistogramme->setEnabled(existDrawing);
    ui->actionBinariserHistogramme->setEnabled(existDrawing);

    //Menu Filtre
    ui->actionMedian->setEnabled(existDrawing);
    ui->actionAttenuate->setEnabled(existDrawing);
    ui->actionConvolution->setEnabled(existDrawing);
    ui->actionDesaturate->setEnabled(existDrawing);
    ui->actionNegative->setEnabled(existDrawing);
    ui->actionSobel->setEnabled(existDrawing);
    ui->actionUpOutline->setEnabled(existDrawing);
    ui->actionNoise->setEnabled(existDrawing);

    // Menu Calque
    ui->actionNewLayer->setEnabled(existDrawing);
    ui->actionRemoveLayer->setEnabled(false);
    ui->actionSelectTheUpperLayer->setEnabled(existDrawing);
    ui->actionSelectTheLowerLayer->setEnabled(existDrawing);
    ui->actionSelectTheUppestLayer->setEnabled(existDrawing);
    ui->actionSelectTheLowestLayer->setEnabled(existDrawing);
    ui->actionTakeUpTheLayer->setEnabled(existDrawing);
    ui->actionTakeDownTheLayer->setEnabled(existDrawing);
    ui->actionLayerFusionVisibleSizeIntersect->setEnabled(existDrawing);
    ui->actionLayerFusionVisibleSizeImage->setEnabled(existDrawing);
    ui->actionLayerFusionVisibleSizeUnion->setEnabled(existDrawing);

    // At the begin the user could use selection tool (default tool)
    selectionRectMode();
}

//***************************************************************************** Gestion of Zoom

void PImage::zoomPlus(){
     DrawingArea* drawing = (DrawingArea*) tabDrawingArea->currentWidget();
     drawing->scale( qreal(1.1), qreal(1.1));
     afficheZoom(drawing);
}

void PImage::zoomMoin(){
    DrawingArea* drawing = (DrawingArea*) tabDrawingArea->currentWidget();
    drawing->scale( qreal(1.0 / 1.1), qreal(1.0 / 1.1));
    afficheZoom(drawing);
}

void PImage::afficheZoom(DrawingArea *draw){
    QString textZoom;
    ui->labelNiveauZoom->setText(textZoom.setNum(((double)draw->getScale())*100)+"%");
}

//***************************************************************************** Menu Fichier

void PImage::newProject(){
    ui_NewProject = new Ui_NewProjectNS::NewProject();
    QDialog* dialogNewProject = new QDialog(this);
    ui_NewProject->setupUi(dialogNewProject);
    dialogNewProject->show();
    QObject::connect(ui_NewProject->buttonBox, SIGNAL(accepted()), this, SLOT(newProjectValidate()));
}

void PImage::newProjectValidate(){
    int valLargeur = ui_NewProject->spinBoxLargeur->value();
    int valHauteur = ui_NewProject->spinBoxHauteur->value();
    DrawingArea* drawing = new DrawingArea(this, valLargeur, valHauteur);

    QString convert;
    tabDrawingArea->addTab(drawing, "NouveauProjet_"+convert.setNum(newProjectCount));
    newProjectCount+=1;

    // Redraw the tabDrawing (without thoses line, the tabwidget does not repaint itself)
    tabDrawingArea->setUpdatesEnabled(true);
    tabDrawingArea->setCurrentWidget(drawing);

    // We enable the button of the menu because now we have a drawing to work on
    enableButtonMenu();

    updateCalque(drawing);
}

void PImage::open(){
    // We opening a window to ask the name and path of the new image to open
    QString fileName = QFileDialog::getOpenFileName(this, "Ouvrir Image", ".", "Fichiers Image (*.png *.jpg *.bmp)");

    if(fileName == NULL){
        // if the user presses Cancel the file name will be null, return without opening anything
        return;
    }

    // We making a drawing area that we put into a new tab of the tabwidget (tabDrawingArea)
    DrawingArea* drawing = new DrawingArea(this);
    drawing->load(fileName);

    // To improve to be more portable
    int numSeparator = fileName.count("/");

    fileName = fileName.section("/", numSeparator, numSeparator + 1);
    tabDrawingArea->addTab(drawing, fileName);

    // Redraw the tabDrawing (without thoses line, the tabwidget does not repaint itself)
    tabDrawingArea->setUpdatesEnabled(true);

    tabDrawingArea->setCurrentWidget(drawing);

    // We enable the button of the menu because now we have a drawing to work on
    enableButtonMenu();
}

void PImage::saveAs(){
    // Opening of a window to ask the name of the new image and the repertory where we will put it
    QString fileName = QFileDialog::getSaveFileName(this, "Enregister Image sous", ".", "Fichiers Image (*.png *.jpg *.bmp)");
    DrawingArea* drawing = (DrawingArea*) tabDrawingArea->currentWidget();
    drawing->save(fileName);
}

void PImage::save(){
    saveAs();
}

void PImage::closeDrawing(){

    DrawingArea* drawing = (DrawingArea*) tabDrawingArea->currentWidget();
    int indexOfdrawing = tabDrawingArea->indexOf(drawing);
    saveAs();
    tabDrawingArea->removeTab(indexOfdrawing);
    delete drawing;

    // Redraw the tabDrawing (without thoses line, the tabwidget does not repaint itself)
    tabDrawingArea->setUpdatesEnabled(true);

    // We enable the button of the menu if we still have a drawing
    enableButtonMenu();

    // We recreate the dock calque empty
    if(tabDrawingArea->count() == 0){
       calque = new Calque(ui->dockCalques);
    }
}

void PImage::closeDrawing(int A){
    saveAs();
    tabDrawingArea->removeTab(A);

    // Redraw the tabDrawing (without thoses line, the tabwidget does not repaint itself)
    tabDrawingArea->setUpdatesEnabled(true);

    // We enable the button of the menu if we still have a drawing
    enableButtonMenu();

    // We recreate the dock calque empty
    if(tabDrawingArea->count() == 0){
       calque = new Calque(ui->dockCalques);
    }
}

void PImage::quit(){
    DrawingArea* drawing = (DrawingArea*) tabDrawingArea->currentWidget();
    if(drawing!=NULL){
        ui_CloseApplication = new Ui_CloseApplicationNS::CloseApplication();
        QDialog* dialogCloseApplication = new QDialog(this);
        ui_CloseApplication->setupUi(dialogCloseApplication);
        dialogCloseApplication->show();
        QObject::connect(ui_CloseApplication->enregistrer, SIGNAL(pressed()), this, SLOT(saveAsClose()));
        QObject::connect(ui_CloseApplication->nePasEnregistrer, SIGNAL(pressed()), this, SLOT(quitConfirme()));
    }else{
        qApp->exit(0);
    }
}

void PImage::saveAsClose(){
    saveAs();
    qApp->exit(0);
}

void PImage::quitConfirme(){
    qApp->exit(0);
}

void PImage::closeEvent(QCloseEvent * event){
   // For call the dialogue befor close application
   event->ignore();
   quit();
}

//***************************************************************************** MEnu Edition

void PImage::copy(){
    if(layerCopy != NULL){
        delete layerCopy;
    }
    DrawingArea* drawing = (DrawingArea*) tabDrawingArea->currentWidget();
    layerCopy = drawing->copy();
}

void PImage::paste(){
    // iI the layer copy is empty, we enable this action
    if(layerCopy != NULL)
    {
        DrawingArea* drawing = (DrawingArea*) tabDrawingArea->currentWidget();
        drawing->paste(layerCopy->copy());

        // Update the calque in the same time
        calque->updateCalque(drawing);
    }
}

void PImage::selectAll()
{
    DrawingArea* drawing = (DrawingArea*) tabDrawingArea->currentWidget();

    drawing->selectAll(ui->dockInfoTool);
}

//****************************************************************************  Menu Image

void PImage::redimensionner(){
     DrawingArea* drawing = (DrawingArea*) tabDrawingArea->currentWidget();
     redimension = new Redimension(drawing, this);
}

//******************************* Miroir
 
void PImage::MiroirHorizontal(){
    DrawingArea* drawing = (DrawingArea*) tabDrawingArea->currentWidget();
    drawing->MiroirHorizontal();
    updateCalque(drawing);
}

void PImage::MiroirVertical(){
    DrawingArea* drawing = (DrawingArea*) tabDrawingArea->currentWidget();
    drawing->MiroirVertical();
    updateCalque(drawing);
}

//******************************* Rotation

void PImage::rotation90Horaire(){
    DrawingArea* drawing = (DrawingArea*) tabDrawingArea->currentWidget();
    drawing->rotate(90);
    updateCalque(drawing);    
}

void PImage::rotation90AntiHoraire(){
    DrawingArea* drawing = (DrawingArea*) tabDrawingArea->currentWidget();
    drawing->rotate(-90);
    updateCalque(drawing);
}

//****************************************************************************  Menu Histogramme

//*******************************AFFICHAGE

void PImage::showHistogram(){
    DrawingArea* drawing = (DrawingArea*) tabDrawingArea->currentWidget();
    histogram = new Histogramme(drawing->getCurrentLayer()->getImage(), Affichage,this);
}

//*******************************EGALIZATION

void PImage::showHistEgalize(){

    DrawingArea* drawing = (DrawingArea*) tabDrawingArea->currentWidget();
    histogram = new Histogramme(drawing->getCurrentLayer()->getImage(), Egalisation, this);

    delete histogram;
    drawing->update();
    updateCalque(drawing);
}

//*******************************SPREAD(ETALER)

void PImage::showHistSpread(){

    DrawingArea* drawing = (DrawingArea*) tabDrawingArea->currentWidget();
    histogram = new Histogramme(drawing->getCurrentLayer()->getImage(), Etalement, this);

    delete histogram;
    drawing->update();
    updateCalque(drawing);
}

//*******************************SHIFT(RECADRAGE)

void PImage::showHistShift(){

    DrawingArea* drawing = (DrawingArea*) tabDrawingArea->currentWidget();
    histogram = new Histogramme(drawing->getCurrentLayer()->getImage(), Recadrage, this);

    updateCalque(drawing);

}

//*******************************BINARIZATION

void PImage::showHistBin(){

    DrawingArea* drawing = (DrawingArea*) tabDrawingArea->currentWidget();
    histogram = new Histogramme(drawing->getCurrentLayer()->getImage(), Binarisation, this);

    updateCalque(drawing);

}

//**************************************************************************** Menu Filtre

void PImage::median(){
    MedianFilter* medianFilter = new MedianFilter();
    DrawingArea* drawing = (DrawingArea*) tabDrawingArea->currentWidget();
    drawing->applyFilter(medianFilter);
    delete medianFilter;
    calque->updateCalque(drawing);
}

void PImage::attenuate(){
    GaussFilter* gaussFilter = new GaussFilter();
    DrawingArea* drawing = (DrawingArea*) tabDrawingArea->currentWidget();
    drawing->applyFilter(gaussFilter);
    delete gaussFilter;
    calque->updateCalque(drawing);
}

void PImage::desaturate(){
    GrayscaleFilter* grayscaleFilter = new GrayscaleFilter();
    DrawingArea* drawing = (DrawingArea*) tabDrawingArea->currentWidget();
    drawing->applyFilter(grayscaleFilter);
    delete grayscaleFilter;
    calque->updateCalque(drawing);
}

void PImage::convolution(){
    MatrixFilter* matrixFilter = new MatrixFilter();
    DrawingArea* drawing = (DrawingArea*) tabDrawingArea->currentWidget();
    drawing->applyFilter(matrixFilter);
    delete matrixFilter;
    calque->updateCalque(drawing);
}

void PImage::negative(){
    NegativeFilter* negativeFilter = new NegativeFilter();
    DrawingArea* drawing = (DrawingArea*) tabDrawingArea->currentWidget();
    drawing->applyFilter(negativeFilter);
    delete negativeFilter;
    calque->updateCalque(drawing);
}

void PImage::sobel(){
    SobelFilter* sobelFilter= new SobelFilter();
    DrawingArea* drawing = (DrawingArea*) tabDrawingArea->currentWidget();
    drawing->applyFilter(sobelFilter);
    delete sobelFilter;
    calque->updateCalque(drawing);
}

void PImage::upOutline(){
    UpOutline* upOutline= new UpOutline();
    DrawingArea* drawing = (DrawingArea*) tabDrawingArea->currentWidget();
    drawing->applyFilter(upOutline);
    delete upOutline;
    calque->updateCalque(drawing);
}

void PImage::noise(){
    DrawingArea* drawing = (DrawingArea*) tabDrawingArea->currentWidget();
    bruitPoivreEtSel = new BruitPoivreEtSel(drawing->getCurrentLayer()->getImage(),this);
}

//**************************************************************************** Menu Affichage

void PImage::toolsWidgetShow(){
    if (ui->actionShowOutils->isChecked() ){
        ui->dockTools->show();
    }else{
        ui->dockTools->hide();
    }
}

void PImage::CalquesShow(){
    if (ui->actionShowCalques->isChecked()){
        ui->dockCalques->show();
    }else{
        ui->dockCalques->hide();
    }
}

void PImage::ColorShow(){
    if (ui->actionShowColors->isChecked()){
        ui->dockColors->show();
    }else{
        ui->dockColors->hide();
    }
}

void PImage::MenuAffichageUpdate(){    
    if (ui->dockTools->isHidden()){
        ui->actionShowOutils->setChecked(false);
    }else{
        ui->actionShowOutils->setChecked(true);
    }
    if (ui->dockColors->isHidden()){
        ui->actionShowColors->setChecked(false);
    }else{
        ui->actionShowColors->setChecked(true);
    }
    if (ui->dockCalques->isHidden()){
        ui->actionShowCalques->setChecked(false);
    }else{
        ui->actionShowCalques->setChecked(true);
    }
}

//************************************************************************** Menu Layers

void PImage::addNewLayer(){
    DrawingArea* drawing = (DrawingArea*) tabDrawingArea->currentWidget();
    drawing->addLayerAfterCurrentLayer();
    updateCalque(drawing);
}

void PImage::removeLayer(){
    DrawingArea* drawing = (DrawingArea*) tabDrawingArea->currentWidget();
    drawing->removeCurrentLayer();
    updateCalque(drawing);
}

void PImage::selectTheLowerLayer(){
    DrawingArea* drawing = (DrawingArea*) tabDrawingArea->currentWidget();
    drawing->selectTheLowerLayer();
}

void PImage::selectTheUpperlayer(){
    DrawingArea* drawing = (DrawingArea*) tabDrawingArea->currentWidget();
    drawing->selectTheUpperlayer();
}

void PImage::selectTheLowestLayer(){
    DrawingArea* drawing = (DrawingArea*) tabDrawingArea->currentWidget();
    drawing->selectTheLowestLayer();
}

void PImage::selectTheUppestLayer(){
    DrawingArea* drawing = (DrawingArea*) tabDrawingArea->currentWidget();
    drawing->selectTheUppestLayer();
}

void PImage::takeDownTheLayer(){
    DrawingArea* drawing = (DrawingArea*) tabDrawingArea->currentWidget();
    drawing->takeDownTheLayer();
}

void PImage::takeUpTheLayer(){
    DrawingArea* drawing = (DrawingArea*) tabDrawingArea->currentWidget();
    drawing->takeUpTheLayer();
}

void PImage::fusionVisibleLayersUnion(){
    DrawingArea* drawing = (DrawingArea*) tabDrawingArea->currentWidget();
    drawing->fusionVisibleLayers(ResizeToUnionLayers);
}

void PImage::fusionVisibleLayersFitImage(){
    DrawingArea* drawing = (DrawingArea*) tabDrawingArea->currentWidget();
    drawing->fusionVisibleLayers(SizeImage);
}

void PImage::fusionVisibleLayersIntersection(){
    DrawingArea* drawing = (DrawingArea*) tabDrawingArea->currentWidget();
    drawing->fusionVisibleLayers(CropToIntersectionLayers);
}

//*****************************************************************************  Menu Aide

void PImage::aPropos(){
    ui_APropos = new Ui_AProposNS::APropos();
    QDialog* dialogAPropos = new QDialog(this);
    ui_APropos->setupUi(dialogAPropos);
    dialogAPropos->show();
}

//*****************************************************************************  Tools

void PImage::croppedMode(){
    for(int i = 0; i < tabDrawingArea->count(); i++)    {
         ((DrawingArea*) tabDrawingArea->widget(i))->setTool(Cropped,ui->dockInfoTool);
    }
}

void PImage::moveMode(){
    for(int i = 0; i < tabDrawingArea->count(); i++)    {
        ((DrawingArea*) tabDrawingArea->widget(i))->setTool(Moved_Tool,ui->dockInfoTool);
    }
}

void PImage::moveModeSelection(){
    for(int i = 0; i < tabDrawingArea->count(); i++)    {
        ((DrawingArea*) tabDrawingArea->widget(i))->setTool(MovedSelection_Tool,ui->dockInfoTool);
    }
}

void PImage::takeColorMode(){
    for(int i = 0; i < tabDrawingArea->count(); i++)    {
        ((DrawingArea*) tabDrawingArea->widget(i))->setTool(Take_Color,ui->dockInfoTool);
    }
}

void PImage::selectionRectMode(){
    for(int i = 0; i < tabDrawingArea->count(); i++)    {
        ((DrawingArea*) tabDrawingArea->widget(i))->setTool(Selection_Rect,ui->dockInfoTool);
    }
}

void PImage::cleverSelectionMode(){
    for(int i = 0; i < tabDrawingArea->count(); i++)    {
        ((DrawingArea*) tabDrawingArea->widget(i))->setTool(Clever_Selection,ui->dockInfoTool);
    }
}

void PImage::pencilMode(){
    for(int i = 0; i < tabDrawingArea->count(); i++)    {
         ((DrawingArea*) tabDrawingArea->widget(i))->setTool(Pencil_Tool,ui->dockInfoTool);
    }
}

//*****************************************************************************  Dock Tools

void PImage::createTools(){

    contentDockWidgetTools = new QWidget();

    uiTools = new ToolsClass();
    uiTools->setupUi(contentDockWidgetTools);
    ui->dockTools->setWidget(contentDockWidgetTools);

    QObject::connect(uiTools->actionCrop , SIGNAL(triggered()), this, SLOT(croppedMode()));
    QObject::connect(uiTools->actionPipette , SIGNAL(triggered()), this, SLOT(takeColorMode()));
    QObject::connect(uiTools->actionMove, SIGNAL(triggered()), this, SLOT(moveMode()));
    QObject::connect(uiTools->actionMoveSelection, SIGNAL(triggered()), this, SLOT(moveModeSelection()));
    QObject::connect(uiTools->actionSelectedRect , SIGNAL(triggered()), this, SLOT(selectionRectMode()));
    QObject::connect(uiTools->actionCleverSelected , SIGNAL(triggered()), this, SLOT(cleverSelectionMode()));
    QObject::connect(uiTools->actionPencil , SIGNAL(triggered()), this, SLOT(pencilMode()));
}

//*****************************************************************************  Dock Calque

void PImage::createCalque(){
    calque = new Calque(ui->dockCalques);
}

void PImage::updateCalque(DrawingArea* drawingParam){
    if (drawingParam == NULL){
        DrawingArea* drawingCurrent = (DrawingArea*) tabDrawingArea->currentWidget();
        calque->updateCalque(drawingCurrent);
    }else{
        calque->updateCalque(drawingParam);
    }

}

//*****************************************************************************  Dock Colors

void PImage::createDockColors(){
    uiColors = new Colors();
    ui->dockColors->setWidget(uiColors);
}

Colors* PImage::getUiColors(){
    return uiColors;
}

//*****************************************************************************  Gestion of tabs

void PImage::changementTab(){
    if(tabDrawingArea->count() != 0)    {
        DrawingArea* drawing = (DrawingArea*) tabDrawingArea->currentWidget();
        calque->updateCalque(drawing);
    }
}

//*****************************************************************************  Drawing

DrawingArea* PImage::getCurrentDrawing(){
    DrawingArea* drawing = (DrawingArea*) tabDrawingArea->currentWidget();
    return drawing;
}

//*****************************************************************************  Destructor

PImage::~PImage(){
    delete ui;
    delete tabDrawingArea;
    delete uiTools;
    delete contentDockWidgetTools;
}
